//
//  EmoticonPackage.swift
//  EmotiDemo
//
//  Created by leador on 17/5/16.
//  Copyright © 2017年 袁满. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {

    var emoticons:[Emoticon] = [Emoticon]()
    
    init(id:String) {
    
        super.init()
        
        if id == "" {
            addemptyEmoticon(isRecently:true)
            return
        }
    
        let plistPath = Bundle.main.path(forResource: "\(id)/info.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        
        let array = NSArray(contentsOfFile: plistPath)! as! [[String:String]]
        
        
        var index = 0
        
        for var emoticonDic in array {
            
            if let png = emoticonDic["png"] {
                
                emoticonDic["png"] = id + "/" + png
        
            }
            
            emoticons.append(Emoticon(dict: emoticonDic))
            
            index = index + 1
            
            if index == 20 {
                emoticons.append(Emoticon(isRemove: true))
                index = 0
            }
            
        }
        
        addemptyEmoticon(isRecently: false)
        
    }
    
    func addemptyEmoticon(isRecently:Bool) {
        
        let count = emoticons.count % 21
        
        if count == 0 && !isRecently {
            
            return
        }
        
        for _ in count..<20 {
            emoticons.append(Emoticon(isEmpty: true))
        }
        
        emoticons.append(Emoticon(isRemove: true))
        
    }
    
}
